package com.qianchang.microfinance.view.manager.base;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.dm.library.utils.DMLog;

/**
 * Created by God on 2016/12/1.
 */
public class BaseViewManager<T> {
    protected Context mContext;
    protected View mView;
    public T mData;

    public BaseViewManager(Context context) {
        mContext = context;
    }

    public View getView() {
        return mView;
    }

    /**
     * 通知activity调用finish方法回退到上一个界面
     */
    protected void back() {
        // 返回
        if (mContext instanceof OnFinishListener) {
            OnFinishListener listener = (OnFinishListener) mContext;
            listener.onFinish();
        }
    }

    /**
     * 检查是否为连续2次的点击事件
     *
     * @param id
     * @return
     */
    protected boolean checkClick(int id) {
        boolean b = true;
        if (mContext instanceof ViewClickCheckListener) {
            ViewClickCheckListener checkListener = (ViewClickCheckListener) mContext;
            b = checkListener.checkClick(id);
        }
        return b;
    }

    protected View.OnClickListener getClickListener() {
        View.OnClickListener clickListener = null;
        if (mContext instanceof View.OnClickListener) {
            clickListener = (View.OnClickListener) mContext;
        }
        return clickListener;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    public void setData(T t) {
    }

    protected String getString(int id) {
        return mContext.getString(id);
    }

    protected View findViewById(int id) {
        DMLog.i(tag, "findViewById() -- mView:" + mView);
        return mView == null ? null : mView.findViewById(id);
    }

    protected void showNoDataView() {
        setNodDataVisible(true);
    }

    protected void hideNoDataView() {
        setNodDataVisible(false);
    }

    private String tag = this.getClass().getSimpleName();

    private void setNodDataVisible(boolean b) {
        View view = mView.findViewById(R.id.activity_error_nodata_container);
        DMLog.i(tag, "setNodtataVisible() -- activity_error_nodata_container is null....");
        if (view != null) {
            view.setVisibility(b ? View.VISIBLE : View.GONE);
        }
    }

    protected void showLoadingDialog() {
        if (mContext instanceof BaseActivity) {
            BaseActivity baseActivity = (BaseActivity) mContext;
            baseActivity.show();
        }
    }

    protected void dissMissLoadingDialog() {
        if (mContext instanceof BaseActivity) {
            BaseActivity baseActivity = (BaseActivity) mContext;
            baseActivity.dismiss();
        }
    }

    public interface OnFinishListener {
        void onFinish();
    }

    public interface ViewClickCheckListener {
        boolean checkClick(int id);
    }

}
